
#include "cxibuttontest.h"

#include <QTest>
#include <QSignalSpy>
#include <QApplication>
#include <controls/button/cxibutton.h>

CxiButtonTest::CxiButtonTest(QWidget* parent) : QObject(parent)
{
}

CxiButtonTest::~CxiButtonTest()
{
}

void CxiButtonTest::initTestCase()
{
	// 准备按钮实例
	m_button = new CxiButton();
	m_button->setText("Test");
	m_button->resize(100, 40);
	m_button->show();
	QVERIFY(QTest::qWaitForWindowExposed(m_button));
}

void CxiButtonTest::tstButtonClicked()
{
	QSignalSpy spy(m_button, &QPushButton::clicked);
	QVERIFY(spy.isValid());

	// 模拟鼠标点击
	QTest::mouseClick(m_button, Qt::LeftButton);
	QCOMPARE(spy.count(), 1);

	// 键盘空格触发点击
	QTest::keyClick(m_button, Qt::Key_Space);
	QCOMPARE(spy.count(), 2);
}

void CxiButtonTest::cleanupTestCase()
{
	if (m_button) {
		m_button->hide();
		m_button->deleteLater();
		m_button = nullptr;
	}
}

QTEST_MAIN(CxiButtonTest)